﻿
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osgDB/ReadFile>
#include <iostream>
/**
* @brief Create a half-transparent box model. Width:500, Height:200, Depth:500, Center:(0,0,0)
* @return Box model node.
*/
osg::ref_ptr<osg::Group> CreateBox()
{
	osg::ref_ptr<osg::Group> gnode = new osg::Group;
	osg::ref_ptr<osg::ShapeDrawable> sdBtm = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0, 0.0, -100.0), 500.0f, 500.0f, 2.0f));
	osg::ref_ptr<osg::ShapeDrawable> sdLft = new osg::ShapeDrawable(new osg::Box(osg::Vec3(-250.0, 0.0, 0.0), 2.0f, 500.0f, 200.0f));
	osg::ref_ptr<osg::ShapeDrawable> sdRgt = new osg::ShapeDrawable(new osg::Box(osg::Vec3(250.0, 0.0, 0.0), 2.0f, 500.0f, 200.0f));
	osg::ref_ptr<osg::ShapeDrawable> sdFrnt = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0, -250.0, 0.0), 500.0f, 2.0f, 200.0f));
	osg::ref_ptr<osg::ShapeDrawable> sdBack = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0, 250.0, 0.0), 500.0f, 2.0f, 200.0f));

	sdBtm->setColor(osg::Vec4f(0.5, 0.5, 0.5, 0.5));
	sdFrnt->setColor(osg::Vec4f(0.5, 0.5, 0.5, 0.5));
	sdBack->setColor(osg::Vec4f(0.5, 0.5, 0.5, 0.5));
	sdLft->setColor(osg::Vec4f(0.5, 0.5, 0.5, 0.5));
	sdRgt->setColor(osg::Vec4f(0.5, 0.5, 0.5, 0.5));

	sdBtm->setName("BoxBottom");
	sdFrnt->setName("BoxFront");
	sdBack->setName("BoxBack");
	sdLft->setName("BoxLeft");
	sdRgt->setName("BoxRight");

	gnode->addChild(sdBtm);
	gnode->addChild(sdFrnt);
	gnode->addChild(sdBack);
	gnode->addChild(sdLft);
	gnode->addChild(sdRgt);

	gnode->setName("BoxGeode");

	gnode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);

	return gnode;
}

/* Export Box node as an ive file. */
void ExportBoxNodeAsIVE()
{
	osg::ref_ptr<osg::Node> node = CreateBox();
	osgDB::ReaderWriter::WriteResult result = osgDB::Registry::instance()->writeNode(*node, "box.ive", osgDB::Registry::instance()->getOptions());
	if (result.success())
	{
		osg::notify(osg::NOTICE) << "Write Node Success" << std::endl;
	}
}

